﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.Sql;
namespace EB.SQL
{
    public enum VariableType
    {
        Integer,
        StringfValue
    }
  public class ValueDetails
    {
        string _value;

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }
        VariableType _type;

        public VariableType Type
        {
            get { return _type; }
            set { _type = value; }
        }
        public ValueDetails(VariableType type)
        {
            Type = type;
        }
    }


    /// <summary>
    /// Summary description for EBSqlInsertHelper
    /// </summary>
    public class SqlInsertHelper
    {
        string _tableName;
        string _keyprimary;
   

        public string TableName
        {
            get { return _tableName; }
            set { _tableName = value; }
        }
        public string KeyPrimary
        {
            get { return _keyprimary; }
            set { _keyprimary = value; }
        }
        string _conString;

        public string ConString
        {
            get { return _conString; }
            set { _conString = value; }
        }
      Dictionary<string, ValueDetails> _fields = new Dictionary<string, ValueDetails>();
       
       public Dictionary<string, ValueDetails> Fields
        {
            get { return _fields; }
            set { _fields = value; }
        }
        public int ExecInsert()
       {
           int id = 0;
            try
            {
             
                string fields = "(";
                string values = " values (";
                int i = 0;
                foreach (string key in _fields.Keys)
                {
                    if (i != 0)
                    {
                        fields += ",";
                        values += ",";
                    }
                    fields += key;
                    ValueDetails value;
                    _fields.TryGetValue(key, out value);

                    if (value.Type == VariableType.StringfValue)
                    {
                        values += ("'" + value.Value + "'");
                    }
                    else
                    {
                        values += value.Value;
                    }
                    ++i;
                }
                fields += ")";
                values += ")";
                string sqlCmd = "insert into " + _tableName + " ";
                sqlCmd += fields;
                sqlCmd += values;
                sqlCmd += ";";
                string sqlCmdNext = "SELECT " + _keyprimary + " FROM " + _tableName + "  WHERE " + _keyprimary + " =@@IDENTITY";
                sqlCmd += sqlCmdNext;
                SqlConnection sqlCon = new SqlConnection();
                SqlCommand sqlCommand = new SqlCommand();

                sqlCon.ConnectionString = _conString;
                sqlCommand.Connection = sqlCon;
                sqlCommand.CommandText = sqlCmd;
                sqlCon.Open();
                id = Convert.ToInt32(sqlCommand.ExecuteScalar());
          
                //int temp = sqlCommand.ExecuteNonQuery();
                sqlCon.Close();
                //if (temp == 1)
                //    return true;
                //else
                //    return false;


                return id;
            }
            catch (Exception ex)
            {

              
            }

            return id;
        }
        
        public SqlInsertHelper()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
}